const cloud = require('wx-server-sdk');
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database();
const utils = require('../utils/index');  

exports.main = async (event, context) => {

  let _openid = event.params._openid;
  let {passTimeArray} = event.params;

  if (passTimeArray.length != 6 ) {
    return false;
  }else{
    await db.collection(utils.collectionName).where({
      _openid
    }).limit(1).get().then((res)=>{

      let dbArray  = res.data[0].passTimeArray;
      console.log(dbArray);
      // 对于传过来的数据进行验证，保证数据有效，并且比以前的时间短或相当
      passTimeArray.forEach((element,index) => {
        if (element > dbArray[index]) {
          passTimeArray[index] = dbArray[index];
        }
      });
      db.collection(utils.collectionName).where({
        _openid
      }).update({
        data:{
          passTimeArray
        }
      }).then((res)=>{
        console.log(res);
        return true;
      })

    })
  }
};